/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.googlecode.jmicroerp.wicket.administrare;

import com.googlecode.jmicroerp.model.Magazin;
import com.googlecode.jmicroerp.model.NTipRecipient;
import com.googlecode.jmicroerp.model.Recipient;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.wicket.DefaultEditPage;

import java.math.BigDecimal;
import java.util.List;

import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Alexandru.Codescu
 */
public class EditareRecipientePage extends DefaultEditPage {
    private Recipient recipient;
    @SpringBean(name = "genericService")
    private GenericService<NTipRecipient> tipRecipientService;
    @SpringBean(name = "genericService")
    private GenericService<Recipient> recipientService;
    @SpringBean(name = "genericService")
    private GenericService<Magazin> magazinService;
    
    public EditareRecipientePage() {
        recipient = new Recipient();
        init();
    }
    
    public EditareRecipientePage(Long id) {
        recipient = recipientService.getById(Recipient.class, id);
        init();
    }
    
    private void init() {
        Form<Recipient> form = new Form<Recipient>("form");
        form.setModel(new CompoundPropertyModel<Recipient>(recipient));
        
        TextField<String> tfDenumire = new TextField<String>("denumire");
        tfDenumire.setRequired(true);
        form.add(tfDenumire);
        
        DropDownChoice<NTipRecipient> ddcTipRecipient = new DropDownChoice<NTipRecipient>("tipRecipient", new LoadableDetachableModel<List<NTipRecipient>>() {

            @Override
            protected List<NTipRecipient> load() {
                return tipRecipientService.getList(NTipRecipient.class, null, null);
            }
        }, new ChoiceRenderer<NTipRecipient>("denumire", "id"));
        form.add(ddcTipRecipient);
        
        DropDownChoice<Magazin> ddcMagazin = new DropDownChoice<Magazin>("magazin", new LoadableDetachableModel<List<Magazin>>() {

            @Override
            protected List<Magazin> load() {
                return magazinService.getList(Magazin.class, null, null);
            }
        }, new ChoiceRenderer<Magazin>("denumire", "id"));
        form.add(ddcMagazin);
        
        TextField<BigDecimal> tfOrdine = new TextField<BigDecimal>("ordine");
        tfOrdine.setRequired(true);
        form.add(tfOrdine);
        
        CheckBox ckActiv = new CheckBox("activ");
        form.add(ckActiv);
        
        form.add(new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (recipient.getId() == null) {
                    recipientService.insert(recipient);
                } else {
                    recipientService.update(recipient);
                }
                setResponsePage(VizualizareRecipientePage.class);
            }
            
        });
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(VizualizareRecipientePage.class);
            }
            
        });
        
        add(form);
    }
}
